Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Complete Code Listing for Employee.as

class Employee
{
	// properties
	public var fName:String;
	public var lName:String;
	public var location:String;
	// constructor
	public function Employee()
	{}
 // methods
   public function addEmpData(fName:String,lName:String,location:String):Void
   {
   	this.fName=fName;
   	this.lName=lName;
   	this.location=location;
   }
}

Complete Code Listing for DataController.as

import mx.managers.DragManager;
import Employee;
class DataController
{
	// constructor
	public function DataController()
	{}
 // methods
  public function addNewEmp(src:Object,dest:Object):Void
   {
   // create an Employee object
   var tmpObj:Employee=new Employee();
   // add the new data to the object properties
   tmpObj.addEmpData(src.fName,src.lName,src.location);
   // add the employee object to the destination dataProvider
   dest.addItem(tmpObj);
   }
	public function doDragEnter(event):Void
   {
   // allow drop
   event.handled=true;
   // set action to copy
   event.action = DragManager.COPY;
   }
 	public function doDragDrop(event):Void
   {
   /*
   event.dragSource.dataForFormat("items") returns
   an array of dragged data items
   */
   var items = event.dragSource.dataForFormat("items");
   // for convenience, set var for drop target
   var dest = event.target;
   // copy the item target's dataProvider
   addNewEmp(items[0],dest);
   }
}